<?php


function parseTime( $type , $time1 ){
	$time1 = trim($time1);
	$type =  trim($type);
	if( $type == "M"){
		$time = DateTime::createFromFormat('Y-m',$time1 );
		if($time){
			$year =  $time->format("Y");
			$month = $time->format("m");
			$time->setTime(0,0,0);
			$time->setDate( $year, $month, 1);
		}
	}
	else if( $type == "D"){
		$time = DateTime::createFromFormat('Y-m-d',$time1 );
		if($time){
			 
			$time->setTime(0,0,0);
			 
		}
	}
	else if( $type == "Y"){
		$time = DateTime::createFromFormat('Y',$time1 ); 
		if($time){
			$year =  $time->format("Y");
			 
			$time->setTime(0,0,0);
			$time->setDate( $year, 1 , 1);
		}
	}
	else 
		$time = DateTime::createFromFormat('Y-m-d H:i:s',$time1 );

	if($time == null)
		throw new Exception("Inviald time format "  . $time1 . " $type" );

	return $time;
}



	$time1 = "2016-03";
 	$time = parseTime('M',$time1 );
 

 	print("$time1  <br/>");
 	var_dump($time); print("<br/>");
 	print($time->format("Y-m-d H:i:s")); 
 	print("<br/>");


 		$time1 = "2016";
 	$time = parseTime('Y',$time1 );
 

 	print("$time1  <br/>");
 	var_dump($time); print("<br/>");
 	print($time->format("Y-m-d H:i:s"));
 	print("<br/>");


 		$time1 = "2016-03-12";
 	$time = parseTime('D',$time1 );
 

 	print("$time1  <br/>");
 	var_dump($time); print("<br/>");
 	print($time->format("Y-m-d H:i:s"));
 print("<br/>");
?>
